package proj1.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;

import proj1.oracle.OracleDB;
import proj1.util.Logger;


public class ApplicationFrame extends JFrame{
	private static final long serialVersionUID = -1449605327877149857L;
	Logger log = Logger.getLogger(ApplicationFrame.class);
	
	// MAIN CONTENT 
	JPanel contentContainer; // Where stuff goes
	JLabel messageContainer; // Messages 
	Container currentPanel; // Ease of use with history
	
	// MENU
	JMenuBar mainMenu; // Top menu (File, Help, etc)

	// NAVIGATION
	LinkedList<Container> history;
	JPanel navigation;
	JButton backButton;
	
	public ApplicationFrame(){
		super();
		// Connect to the database
		OracleDB.getInstance();
		
		history = new LinkedList<Container>();
		
		// CREATE THE MENU
		mainMenu = new JMenuBar();
		mainMenu.setBorder(new BevelBorder(BevelBorder.RAISED));
		
		// File Menu
		JMenu file = new JMenu();
		JMenuItem exitMenu = new JMenuItem();
		file.setText("File");
		exitMenu.setText("Exit");
		mainMenu.add(file);
		file.add(exitMenu);
		
		// Help Menu
		JMenu help = new JMenu();		
		JMenuItem aboutMenu = new JMenuItem();
		help.setText("Help");
		aboutMenu.setText("About");
		mainMenu.add(help);
		help.add(aboutMenu);
	
		// CREATE PLACEHOLDER FOR MESSAGES
		messageContainer = new JLabel();
		messageContainer.setHorizontalAlignment(SwingConstants.CENTER);
		messageContainer.setVisible(true);
		
		// CREATE CONTENT CONTAINER
		contentContainer = new JPanel();
		contentContainer.setBorder(BorderFactory.createBevelBorder(TitledBorder.LEFT));
		
		// CREATE THE NAVIGATION
		navigation = new JPanel();
		navigation.setAlignmentX(CENTER_ALIGNMENT);
		navigation.setAlignmentY(BOTTOM_ALIGNMENT);
		backButton = new JButton("Back");
		backButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				back();
			}
		});
		navigation.add(backButton);
		
		// BUILD THE FRAME CONTENTS
		setLayout(new BorderLayout());
		setJMenuBar(mainMenu);
		add(messageContainer, BorderLayout.NORTH);
		add(contentContainer, BorderLayout.CENTER);
		add(navigation, BorderLayout.PAGE_END);
		setContentPane(new MainMenu(this), false);
		setMinimumSize(new Dimension(800,500));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	// Reloads the last pane from history
	public void back(){
		setContentPane(history.removeLast(), false);
		if(history.isEmpty())
			backButton.setEnabled(false);
	}
	
	// Sets the main pane and puts whatever is there currently into the history
	@Override
	public void setContentPane(Container content){	
		setContentPane(content,true);
	}
	
	public void replaceFrame(Container content) {
		setContentPane(content, false);
	}
	
	private void setContentPane(Container content, boolean addToHistory){
		if(currentPanel != null && addToHistory)
			history.add(currentPanel);
		
		if(currentPanel != null)
			currentPanel.setVisible(false);

		// Change pointer for current panel
		currentPanel = content;
	
		// Update the pane's content
		contentContainer.removeAll();
		contentContainer.add(content);
		contentContainer.setVisible(true);
		currentPanel.setVisible(true);
		
		// Always delete message contents on load
		setMessage(currentPanel.toString());
		
		// If there is anything in history, enable the back button. Otherwise, disable it
		backButton.setEnabled(!history.isEmpty());
	}
	
	public static void main(String... args){
		ApplicationFrame mainFrame = new ApplicationFrame();
		mainFrame.setLocationRelativeTo(null);  //center
		mainFrame.setVisible(true);
	}

	public void setMessage(String message){
		setMessageContents(message, Color.BLACK);
	}
	
	public void setErrorMessage(String message) {
		setMessageContents(message, Color.RED);
	}
	
	private void setMessageContents(String message, Color color){
		messageContainer.setText(message);
		messageContainer.setForeground(color);
		messageContainer.removeAll();
	}

	public String getMessage() {
		return messageContainer.getText();
	}

	public Dimension getPanelSize(){
		int height = messageContainer.getX() + messageContainer.getHeight() + backButton.getX();
		return new Dimension(this.getWidth(), height);
	}
}
